<template>
  <Dialog
    title="分配客户"
    v-model="dialogVisible"
    align-center
    width="800px"
  >
    <el-row :gutter="20">
      <!-- 左侧部门树 -->
      <el-col :span="6" :xs="24">
        <ContentWrap class="h-1/1">
          <DeptTree @node-click="handleDeptNodeClick" />
        </ContentWrap>
      </el-col>
      <el-col :span="12" :xs="24">
        <ContentWrap class="customer-list-area h-1/1">
          <el-radio-group
            :model-value="followUp.id"
            style="height: 100%;"
            @change="handleChangeFollowUpId"
          >
            <el-radio
              v-for="item in list"
              :key="item.id"
              :value="item.id"
              :label="item.nickname"
            />
          </el-radio-group>
          <el-Pagination
            :total="total"
            small
            layout="prev,pager,next"
            v-model:current-page="queryParams.pageNo"
            v-model:page-size="queryParams.pageSize"
            @current-change="handleChangePageNo"
            style="justify-content: center;"
          />
        </ContentWrap>
      </el-col>
      <el-col :span="6" :xs="24">
        <template v-if="Boolean(followUp.id)">
          <el-form-item label="手机号" label-position="top">
            {{ followUp.mobile || '--' }}
          </el-form-item>
          <el-form-item label="昵称" label-position="top">
            {{ followUp.nickname }}
          </el-form-item>
        </template>
        <template v-else>请选择</template>
      </el-col>
    </el-row>
    <template #footer>
      <el-button
        @click="() => emit('submit', Object.assign({ followUp }, formState))"
        type="primary"
        :disabled="formLoading"
      >
        提 交
      </el-button>
      <el-button
        @click="() => close()"
      >
        取 消
      </el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as UserApi from '@/api/system/user'
import DeptTree from '../user/DeptTree.vue';
import { customer } from '@/api/system/customer';

const emit = defineEmits(['submit']);

const dialogVisible = ref(false);
const formState = ref({
  ids: [],
  customerType: undefined,
});
const ids = ref([]);
function open(outerFormState) {
  formState.value = outerFormState;
  followUp.value = { id: '' };
  dialogVisible.value = true;
  getList();
}

function close() {
  dialogVisible.value = false;
}

const formLoading = ref(false);

const followUp = ref({ id: '' });
function handleChangeFollowUpId(id) {
  followUp.value = list.value.find(item => item.id == id);
}

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list: Ref<customer[]> = ref([]) // 列表的数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  username: undefined,
  mobile: undefined,
  status: undefined,
  deptId: undefined,
  createTime: []
})

function handleChangePageNo(pageNo) {
  queryParams.pageNo = pageNo;
  getList();
}

async function getList() {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 处理部门被点击 */
function handleDeptNodeClick(row: any) {
  queryParams.deptId = row && row.id;
  getList()
}

defineExpose({ open, close });
</script>

<style scoped>
.customer-list-area {
  /* height: 100%; */
}

.customer-list-area :deep(.el-card__body) {
  display: grid;
  grid-template-rows: minmax(0,1fr) auto;
  height: 100%;
}

.el-radio-group {
  display: grid;
  gap: 6px;
  grid-auto-rows: max-content;
  grid-template-columns: repeat(3,minmax(0,1fr));
}

.el-radio { margin: 0; }
</style>
